package com.zhanbo.zhanbowymusic.main.featured;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.PlayListBean;
import com.mwx.baselib.bean.PlayListSongBean;
import com.mwx.baselib.bean.TagPlayListBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.DateUtils;
import com.mwx.baselib.utils.GlideUtils;
import com.mwx.baselib.view.Divider;
import com.mwx.baselib.view.DividerItemDecoration;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentPCZoneBinding;
import com.zhanbo.zhanbowymusic.main.bean.CreatePlaylistDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.bean.FeaturedPlaylistDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.bean.PCZoneListDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.featured.adapter.PCZoneListAdapter;
import com.zhanbo.zhanbowymusic.main.mine.create.adapter.CreatePlayListAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 亲子专区
 */
public class PCZoneFragment extends BaseFragment<FragmentPCZoneBinding> {
    @Override
    protected Object getLayout() {
        return R.layout.fragment_p_c_zone;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initData() {
        Map<String,String> map = new HashMap<>();

            NetworkManager.getInstance().getApiService().tagPlayList(map)
                    .subscribeOn(Schedulers.io())
                    .compose(this.bindToLifecycle())
                    .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<List<TagPlayListBean>>>(_mActivity) {
                        @Override
                        public void onSuccess(BaseResponses<List<TagPlayListBean>> listBaseResponses) {
                            binding.rvPCZone.setLayoutManager(new GridLayoutManager(_mActivity, 5));
                            PCZoneListAdapter adapter = new PCZoneListAdapter(_mActivity);
                            adapter.getItems().addAll(listBaseResponses.getData());
                            binding.rvPCZone.setAdapter(adapter);
                            adapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<TagPlayListBean>() {
                                @Override
                                public void onItemClick(int position) {
                                    EventBus.getDefault().post(new FeaturedPlaylistDetailsEBBean(adapter.getItems().get(position).getSong()));
                                }
                            });

                        }
                    });

    }

    @Override
    protected void initView() {
        DividerItemDecoration dividerItemDecoration= new DividerItemDecoration();
        dividerItemDecoration.setDividerLookup(new DividerItemDecoration.DividerLookup() {
            @Override
            public Divider getVerticalDivider(int position) {
                return new Divider.Builder()
                        .size(20)
                        .color(Color.TRANSPARENT)
                        .build();
            }

            @Override
            public Divider getHorizontalDivider(int position) {
                return new Divider.Builder()
                        .size(20)
                        .color(Color.TRANSPARENT)
                        .build();
            }
        });
        binding.rvPCZone.addItemDecoration(dividerItemDecoration);
    }

    @Override
    protected void initListener() {
        binding.iconBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop();
            }
        });
    }

    public static PCZoneFragment newInstance() {

        Bundle args = new Bundle();

        PCZoneFragment fragment = new PCZoneFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
